module.exports = {
    DEFAULT_MUSIC:0.2,
    DEFAULT_EFFECT:0.5,
    DEFAULT_MUSIC_SWITCH:true,
    DEFAULT_EFFECT_SWITCH:true,
    DEFAULT_SHOCK:true,
    shock:true,
    checkSetting(){
        let setting = he.local.getJson("setting")
        if(!setting){
            setting = {
                music_switch : this.DEFAULT_MUSIC_SWITCH,
                effect_switch : this.DEFAULT_EFFECT_SWITCH,
                music : this.DEFAULT_MUSIC, 
                effect : this.DEFAULT_EFFECT,
                shock : this.DEFAULT_SHOCK,
                bgm: 'voice/game'
            }
            he.local.setJson('setting',setting)
        }
        this.defaultSetting()
    },
    defaultSetting(){
        let setting = he.local.getJson('setting')
        if(setting.music_switch){
          cc.audioEngine.setMusicVolume(setting.music)
        }else{
          cc.audioEngine.setMusicVolume(0)
        }
        if(setting.effect_switch){
          cc.audioEngine.setEffectsVolume(setting.effect)
        }else{
          cc.audioEngine.setEffectsVolume(0)
        }
        this.shock = setting.shock
    },
    playMusic(url,bundle,loop = true){
        if(!url){
            url = he.local.get('bgm','setting')
        }
        let clip = he.load.get(url,cc.AudioClip,bundle)
        if(clip){
            cc.audioEngine.playMusic(clip, true);
        }else{
            he.load.getRes(url,cc.AudioClip,(err, clip) => {
                cc.audioEngine.playMusic(clip, loop);
            },bundle)
        }
    },
    stopMusic(url){
        cc.audioEngine.stopMusic();
    },
    pauseVoice(status){
        if(status){
            cc.audioEngine.pauseMusic()
            cc.audioEngine.setEffectsVolume(0)
        }else{
            let effectVolume = he.local.get('effect','setting')
            cc.audioEngine.resumeMusic()
            cc.audioEngine.setEffectsVolume(effectVolume)
        }
    },
    playEffect(url,bundle){
        if(!url){
            he.log("未设置音效路径")
            return null
        }
        let clip = he.load.get(url,cc.AudioClip,bundle)
        if(clip){
            return cc.audioEngine.playEffect(clip, false);
        }else{
            return he.load.getRes(url,cc.AudioClip,function (err, clip) {
                cc.audioEngine.playEffect(clip, false);
            },bundle)
        }
        return null
    },
    vibrateShort(){
      if(this.shock){
        return
      }
      if(window.wx){
          wx.vibrateShort()
      }
    },
    vibrateLong(){
      if(this.shock){
        return
      }
      if(window.wx){
          wx.vibrateLong()
      }
  }
}